﻿using System;
using UnityEngine;
using WebSocketSharp.Server;

internal class ServerManager
{
    private static WebSocketServer WebSocketServers = null;

    public static bool IsListening
    {
        get
        {
            if (WebSocketServers == null)
                return false;
            return WebSocketServers.IsListening;
        }
    }

    private static void Init()
    {
        WebSocketServers = new WebSocketServer(8888);

        //添加具有指定行为和路径的WebSocket服务
        //string 参数 表示要添加的服务的绝对路径
        WebSocketServers.AddWebSocketService<Laputa>("/Laputa");
    }

    public static void Open()
    {
        WebSocketServers.Start();
        Debug.Log("开启服务器");
    }

    public static void Close()
    {
        WebSocketServers.Stop();
        Debug.Log("关闭服务器");
    }

    static ServerManager()
    {
        Init();
    }

    private ServerManager()
    {
    }
}